package com.ecric.http.webView;

import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.ecric.http.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_webview_test)
public class WebViewJSAndroidActivity extends AppCompatActivity {

    @ViewById(R.id.webview)
    WebView webView;

    @AfterViews
    void initData(){
        //设置WebView属性，能够执行JavaScript脚本
        webView.getSettings().setJavaScriptEnabled(true);
        //设置web视图客户端
        webView.setWebViewClient(new MyWebViewClient());

        WebSettings webSettings = webView.getSettings();
        //开启JS调用
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setAllowFileAccessFromFileURLs(true);
        //将MyObject暴露给JS，然后JS通过myObj调用MYObject中的方法
        webView.addJavascriptInterface(new MyObject(this),"myObj");
        //加载URL内容
        webView.loadUrl("file:///android_asset/test.html");

        webView.setWebViewClient(new MyWebViewClient());

    }


}
